/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SitioForm.java
 *
 * Created on 29/03/2010, 11:44:57 AM
 */

package agencia.form;

import agencia.LugarTuristico;
import agencia.TipoLugarTuristico;
import agencia.controller.LugarTuristicoController;
import agencia.controller.TipoLugarcontroller;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class SitioListaForm extends javax.swing.JInternalFrame {

    private JDesktopPane jdesktopPane;
    private LugarTuristicoController sitioController;
    private TipoLugarcontroller tipoLugarcontroller;
    
    public void setSitioController(LugarTuristicoController sitioController) {
        this.sitioController = sitioController;
    }
    public void setJdesktopPane(JDesktopPane jdesktopPane) {
        this.jdesktopPane = jdesktopPane;
    }
    public void setTipoLugarcontroller(TipoLugarcontroller tipoLugarcontroller) {
        this.tipoLugarcontroller = tipoLugarcontroller;
    }
    /** Creates new form SitioForm */
    public SitioListaForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblLugarTuris = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbxTipoSitio = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Sitio Turistico");

        tblLugarTuris.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Tipo"
            }
        ));
        jScrollPane1.setViewportView(tblLugarTuris);

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("E");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("-");

        jLabel1.setText("Tipo de Sitio");

        cbxTipoSitio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton4.setText("Buscar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(199, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(cbxTipoSitio, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbxTipoSitio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // nuevo
        SitioDatosForm form = new SitioDatosForm();
        form.setSitioListaForm(this);
        form.setLugarTuristicoController(sitioController);
        form.setTipoLugarcontroller(tipoLugarcontroller);
        form.setAccion(SitioDatosForm.ACCION_NUEVO);
        jdesktopPane.add(form);
        form.iniciaForm();
        form.show();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

         if (tblLugarTuris.getSelectedRow()!=-1){
           int cod = (Integer)tblLugarTuris.getValueAt( tblLugarTuris.getSelectedRow(), 0);
           SitioDatosForm form = new SitioDatosForm();
            form.setSitioListaForm(this);
            form.setLugarTuristicoController(sitioController);
            form.setTipoLugarcontroller(tipoLugarcontroller);
            form.setAccion(SitioDatosForm.ACCION_MODIFICAR);
            form.setLugarTuristico(sitioController.obtenerLugarturistico(cod));
            form.iniciaForm();
            jdesktopPane.add(form);
            form.show();
         }else{
            JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el guia a modificar");
         }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(cbxTipoSitio.getSelectedItem().toString().equals("Todas")){
            pintagrilla(sitioController.listaLugaresTuristicos());
        }else{
            pintagrilla(sitioController.listaLugaresTuristicos(cbxTipoSitio.getSelectedItem().toString()) );
        }
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxTipoSitio;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblLugarTuris;
    // End of variables declaration//GEN-END:variables

      public void pintagrilla(List<LugarTuristico> examenes){
        DefaultTableModel tabla = (DefaultTableModel) tblLugarTuris.getModel();
        tabla.setRowCount(0);
        for (LugarTuristico examen : examenes) {
            Object[] fila = new Object[3];
            fila[0] = examen.getCodigo();
            fila[1] = examen.getNombre();
            fila[2] = examen.getTipoLugarTuristico().getNombre();
            tabla.addRow(fila);
        }
    }

    public void iniciarForm() {

        cbxTipoSitio.removeAllItems();
        cbxTipoSitio.addItem("Todas");
        for(TipoLugarTuristico tipo : tipoLugarcontroller.getListaTipoLugar()){
            cbxTipoSitio.addItem(tipo.getNombre());
        }
        pintagrilla(sitioController.listaLugaresTuristicos());
    }

    
}
